#include "SysclkClockSignal.h" // IWYU pragma: keep
#include <stdexcept>

void bsp::SysclkClockSignal::Configure(std::string const &input_channel_name)
{
	RCC_ClkInitTypeDef def{};
	def.ClockType = RCC_CLOCKTYPE_SYSCLK;

	if (input_channel_name == "hse")
	{
		def.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
	}
	else if (input_channel_name == "hsi")
	{
		def.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
	}
	else if (input_channel_name == "pll")
	{
		def.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
	}
	else
	{
		throw std::invalid_argument{"不支持此时钟源"};
	}

	HAL_StatusTypeDef ret = HAL_RCC_ClockConfig(&def,
												FLASH_LATENCY_2);

	if (ret != HAL_StatusTypeDef::HAL_OK)
	{
		throw std::runtime_error{"时钟信号配置失败"};
	}
}
